package gl.web.apk.common.config;


import gl.web.apk.common.model._MappingKit;
import gl.web.apk.controller.ApkController;
import gl.web.apk.controller.BlogController;
import gl.web.apk.controller.DownloadController;
import gl.web.apk.controller.UploadController;
import gl.web.apk.index.IndexController;
import gl.web.onlinedoc.controller.OnlineDocController;

import java.io.File;

import com.jfinal.config.Constants;
import com.jfinal.config.Handlers;
import com.jfinal.config.Interceptors;
import com.jfinal.config.JFinalConfig;
import com.jfinal.config.Plugins;
import com.jfinal.config.Routes;
import com.jfinal.ext.handler.ContextPathHandler;
import com.jfinal.kit.PropKit;
import com.jfinal.log.Log;
import com.jfinal.plugin.activerecord.ActiveRecordPlugin;
import com.jfinal.plugin.c3p0.C3p0Plugin;

/**
 * API引导式配置
 */
public class Config extends JFinalConfig {
	private Log log = Log.getLog(getClass().getName());
	/**
	 * 配置常量
	 */
	public void configConstant(Constants me) {
		// 加载少量必要配置，随后可用PropKit.get(...)获取值
		PropKit.use("a_little_config.txt");
		me.setDevMode(PropKit.getBoolean("devMode", false));
		me.setMaxPostSize(100*1024*1024);
		log.info("================= Jfinal Config ======================");
		log.info("updaload dir:"+getImgDir());
		log.info("jdbc url:"+PropKit.get("jdbcUrl"));
		log.info("devMode:"+ PropKit.getBoolean("devMode", false));
		log.info("MaxPostSize:"+ me.getMaxPostSize());
		log.info("================= ============= ======================");
	}
	
	/**
	 * 配置路由
	 */
	public void configRoute(Routes me) {
		me.add("/", IndexController.class, "/index");	// 第三个参数为该Controller的视图存放路径
		me.add("/blog", BlogController.class);			// 第三个参数省略时默认与第一个参数值相同，在此即为 "/blog"
		me.add("/apk", ApkController.class);		
		me.add("/upload", UploadController.class);		
		me.add("/download", DownloadController.class);		
		me.add("/onlinedoc", OnlineDocController.class);		
	}
	
	public static C3p0Plugin createC3p0Plugin() {
		return new C3p0Plugin(PropKit.get("jdbcUrl"), PropKit.get("user"), PropKit.get("password").trim());
	}
	
	/**
	 * 配置插件
	 */
	public void configPlugin(Plugins me) {
		// 配置C3p0数据库连接池插件
		C3p0Plugin C3p0Plugin = createC3p0Plugin();
		me.add(C3p0Plugin);
		
		// 配置ActiveRecord插件
		ActiveRecordPlugin arp = new ActiveRecordPlugin(C3p0Plugin);
		me.add(arp);
		
		// 所有配置在 MappingKit 中搞定
		_MappingKit.mapping(arp);
	}
	
	/**
	 * 配置全局拦截器
	 */
	public void configInterceptor(Interceptors me) {
		
	}
	
	/**
	 * 配置处理器
	 */
	public void configHandler(Handlers me) {
		me.add(new ContextPathHandler("ProjectName"));
	}

	public static String getImgDir() {
		return new File("").getAbsolutePath()+"/";
	}
	public static String getWebRootPath() {
		String classesPath = Thread.currentThread().getContextClassLoader().getResource("").toString();
		classesPath=classesPath.replace("file:/", ""); //去掉file:  
		return classesPath.substring(0,classesPath.indexOf("WEB-INF")); 
	}
	public static File getDocDir() {
		return new File(Config.getWebRootPath()+"/doc"); 
	}
}
